package com.qk.controller;

import com.qk.aop.anno.LogRecord;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.constant.ClueStatusConstants;
import com.qk.domain.clue.ClueDO;
import com.qk.dto.clue.ClueDTO;
import com.qk.dto.clue.ClueFalseDTO;
import com.qk.dto.clue.ClueInfoUpdateDTO;
import com.qk.dto.clue.ClueQueryDTO;
import com.qk.service.ClueService;
import com.qk.vo.clue.ClueVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhangchuang
 * @Date: 2025/10/14 11:34
 * @Description: 线索控制器
 */
@Slf4j
@RestController
@RequestMapping("/clues")
@SuppressWarnings("all")
public class ClueController {

    @Autowired
    private ClueService clueService;

    @GetMapping("/pool")
    public Result getCluePool(ClueQueryDTO clueQueryDTO) {
        log.info("查询线索池，{}",  clueQueryDTO);
        clueQueryDTO.setStatus(ClueStatusConstants.FALSE_CLUE);
        return getClueByPage(clueQueryDTO);
    }

    @LogRecord
    @PutMapping("/toBusiness/{id}")
    public Result updateClueToBusiness(@PathVariable Integer id) {
        log.info("线索转商机，参数：id={}", id);
        return clueService.updateClueToBusiness(id) ? Result.success() : Result.error();
    }

    @LogRecord
    @PutMapping("/false/{id}")
    public Result updateClueToFalse(@PathVariable Integer id, @RequestBody ClueFalseDTO clueFalseDTO) {
        log.info("线索转无效，参数：id={}, reason={}, remark={}", id, clueFalseDTO.getReason(), clueFalseDTO.getRemark());
        clueFalseDTO.setId(id);
        return clueService.updateClueToFalse(clueFalseDTO) ? Result.success() : Result.error();
    }

    @LogRecord
    @PutMapping
    public Result updateClueInfo(@RequestBody ClueInfoUpdateDTO clueInfoUpdateDTO) {
        log.info("修改线索，参数：{}", clueInfoUpdateDTO);
        return clueService.updateClue(clueInfoUpdateDTO) ? Result.success() : Result.error();
    }

    @GetMapping("/{id}")
    public Result getClueInfoById(@PathVariable Integer id) {
        log.info("获取线索，参数：id={}", id);
        ClueVO clueVO = clueService.getClueInfoById(id);
        return  Result.success(clueVO);
    }

    @LogRecord
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assignClue(@PathVariable Integer clueId, @PathVariable Integer userId) {
        log.info("分配线索，参数：clueId={}, userId={}", clueId, userId);
        return clueService.assignClue(clueId, userId) ? Result.success() : Result.error();
    }

    @LogRecord
    @PostMapping
    public Result addClue(@RequestBody ClueDTO clueDTO) {
        log.info("新增线索，参数：{}", clueDTO);
        return clueService.addClue(clueDTO) ? Result.success() : Result.error();
    }

    @GetMapping
    public Result getClueByPage(ClueQueryDTO clueQueryDTO) {
        log.info("查询线索，参数：{}", clueQueryDTO);
        PageResult<ClueDO> list = clueService.getClueByPage(clueQueryDTO);
        return Result.success(list);
    }

}
